package dao

import (
	"fmt"
	"frontend-server/db"
	model "frontend-server/model/gv_im"
	"log"
)

// 根据用户id和房间id查询用户
func GetGvImUserRoomByUserIDRoomID(userID, RoomID uint) (model.GvImUserRoom, error) {
	db := db.GetImDB()
	userRoom := model.GvImUserRoom{}
	err := db.Table("gv_im_user_room").Where("user_id = ? AND room_id = ?", userID, RoomID).First(&userRoom).Error
	return userRoom, err
}

// 根据房间id查询房间哪些用户
func GetGvImUserRoomByRoomID(RoomID uint) ([]model.GvImUserRoom, error) {
	db := db.GetImDB()
	userRooms := []model.GvImUserRoom{}
	err := db.Table("gv_im_user_room").Where("room_id = ?", RoomID).Find(&userRooms).Error
	if err != nil {
		return nil, err
	}
	return userRooms, nil
}

// 查询用户 id 和对方 id 是否为好友
func GetGvImUserRoomByIsFriend(userID, friendUserID uint) bool {
	db := db.GetImDB()
	roomIDs := make([]uint, 0)
	// 查询userID单聊房间列表
	rows, err := db.Table("gv_im_user_room").Where("user_id = ? AND room_type = ?", userID, 1).Rows()
	if err != nil {
		log.Printf("[DB ERROR]:%v\n", err)
		return false
	}
	defer rows.Close()
	for rows.Next() {
		ur := model.GvImUserRoom{}
		err := db.ScanRows(rows, &ur)
		if err != nil {
			return false
		}
		roomIDs = append(roomIDs, ur.RoomID)
	}
	// 获取关联friendUserID单聊房间个数
	var count int64
	err = db.Table("gv_im_user_room").Where("(user_id = ?) IN (room_id = ?)", friendUserID, roomIDs).Count(&count).Error
	if err != nil {
		log.Printf("[DB ERROR]:%v\n", err)
		return false
	}
	if count > 0 {
		return true
	}
	return false
}

// 查询创建者id和用户id列表是否为好友
func GetGvImUserRoomByIsFriends(userID uint, friendUserIDs []uint) bool {
	db := db.GetImDB()
	userRooms := []model.GvImUserRoom{}
	userIDS := make([]uint, 0)
	rows, err := db.Table("gv_im_user_room").Where("room_type = ?", 1).Rows()
	if err != nil {
		fmt.Println(err)
		return false
	}
	defer rows.Close()
	for rows.Next() {
		var userRoom model.GvImUserRoom
		db.ScanRows(rows, &userRoom)
		if userRoom.UserID != userID {
			userRooms = append(userRooms, userRoom)
		}
		// userRooms = append(userRooms, userRoom)
	}
	fmt.Println(userRooms, friendUserIDs)
	for _, ur := range userRooms {
		userIDS = append(userIDS, ur.UserID)
	}
	fmt.Println(userIDS)

	for _, j := range userIDS {
		for _, i := range friendUserIDs {
			// userIDS.
			if j == i {
				return true
			} else {
				return false
			}
		}

	}
	return false
}

// 根据用户 id 和好友 id 查询房间 id
func GetGvImUserRoomByID(userID, friendUserID uint) uint {
	db := db.GetImDB()
	roomIDs := make([]uint, 0)
	// 查询userID单聊房间列表
	rows, err := db.Table("gv_im_user_room").Where("user_id = ? AND room_type = ?", userID, 1).Rows()
	if err != nil {
		log.Printf("[DB ERROR]:%v\n", err)
		return 0
	}
	defer rows.Close()
	for rows.Next() {
		ur := model.GvImUserRoom{}
		err := db.ScanRows(rows, &ur)
		if err != nil {
			return 0
		}
		roomIDs = append(roomIDs, ur.RoomID)
	}
	// 获取关联friendUserID单聊房间个数
	var userRoom = model.GvImUserRoom{}
	err = db.Table("gv_im_user_room").Where("(user_id = ?) IN (room_id = ?)", friendUserID, roomIDs).First(&userRoom).Error
	if err != nil {
		log.Printf("[DB ERROR]:%v\n", err)
		return 0
	}
	return userRoom.RoomID
}

// 创建单聊房间关联
func CreateGvImUserRoom(room model.GvImUserRoom) error {
	db := db.GetImDB()
	err := db.Table("gv_im_user_room").Create(&room).Error
	return err
}

// 创建群聊房间关联
func CreateGvImUserRooms(roomID uint, users []model.GvImUser) error {
	db := db.GetImDB()
	var err error
	for _, value := range users {
		userRoom := model.GvImUserRoom{
			UserID:   value.ID,
			RoomID:   roomID,
			RoomType: 2,
		}
		err = db.Table("gv_im_user_room").Create(&userRoom).Error
	}
	return err
}

// 根据房间 id 删除双方用户
func DeleteGvImUserRoomByID(roomID uint) error {
	db := db.GetImDB()
	err := db.Table("gv_im_user_room").Unscoped().Delete(&model.GvImUserRoom{
		RoomID: roomID,
	}, "room_id = ?", roomID).Error
	return err
}

// 根据用户id查询房间列表 room_type = 2
func GetGvImUserRoomByUserIDRoomType2(userID uint) ([]model.GvImUserRoom, error) {
	db := db.GetImDB()
	userRooms := []model.GvImUserRoom{}
	err := db.Table("gv_im_user_room").Where("user_id = ? AND room_type = ?", userID, 2).Find(&userRooms).Error
	return userRooms, err
}

// 根据用户id查询房间列表 room_type = 1
func GetGvImUserRoomByUserIDRoomType1(userID uint) ([]model.GvImUserRoom, error) {
	db := db.GetImDB()
	userRooms := []model.GvImUserRoom{}
	err := db.Table("gv_im_user_room").Where("user_id = ? AND room_type = ?", userID, 1).Find(&userRooms).Error
	return userRooms, err
}

// 根据用户房间id列表查询用户列表
func GetGvImUserRoomByUserRooms(userRooms []model.GvImUserRoom) ([]model.GvImUserRoom, error) {
	db := db.GetImDB()
	userFriendRooms := []model.GvImUserRoom{}
	var err error
	for _, ur := range userRooms {
		urs := []model.GvImUserRoom{}
		err = db.Table("gv_im_user_room").Where("room_id = ?", ur.RoomID).Find(&urs).Error
		if err != nil {
			return nil, err
		}
		if urs[0].UserID == userRooms[0].UserID {
			userFriendRooms = append(userFriendRooms, urs[1])
		}
	}
	return userFriendRooms, nil
}
